package com.joplus.housekeeper.ui.view.joExpendPopWindow;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.LinearLayout;

import com.joplus.housekeeper.utils.ScreenUtils;

/**
 *为了城市选择框在城市数量很少的情况下设计的视图容器
 * 限制了最大高度为屏幕的1/3
 *author wutingxi
 *time 2016/9/23 16:50
 */
public class MaxHeightVIewGroup extends LinearLayout {
    Context mcontext;
    int mMaxHeight;
    
    public MaxHeightVIewGroup(Context context) {
        super(context);
        mcontext=context;
        mMaxHeight= ScreenUtils.getScreenHeight(mcontext) / 3;
    }

    public MaxHeightVIewGroup(Context context, AttributeSet attrs) {
        super(context, attrs);
        mcontext=context;
        mMaxHeight= ScreenUtils.getScreenHeight(mcontext) / 3;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        if (heightMode == MeasureSpec.EXACTLY) {
            heightSize = heightSize <= mMaxHeight ? heightSize
                    : (int) mMaxHeight;
        }

        if (heightMode == MeasureSpec.UNSPECIFIED) {
            heightSize = heightSize <= mMaxHeight ? heightSize
                    : (int) mMaxHeight;
        }
        if (heightMode == MeasureSpec.AT_MOST) {
            heightSize = heightSize <= mMaxHeight ? heightSize
                    : (int) mMaxHeight;
        }
        int maxHeightMeasureSpec = MeasureSpec.makeMeasureSpec(heightSize,
                heightMode);
        super.onMeasure(widthMeasureSpec, maxHeightMeasureSpec);
    }
}
